layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 盘点明细表管理
     */
    var PandianJihuaDetailed = {
        tableId: "pandianJihuaDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    PandianJihuaDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'inventory', sort: true, title: '状态',
            	templet: function(d){
            		var str = "";
            		if (d.inventory == '待处理') {
                    	str += '<span style="font-weight: bold;color:#e84d74;">' + d.inventory + '</span>'
					} else if (d.inventory == '已处理'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.inventory + '</span>'
					} else if (d.inventory == '已盘'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.inventory + '</span>'
					} 
            		return str
            	}
			},
            {field: 'panType', sort: true, title: '类型',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.panType + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.panType + '</span>'
					} 
            		return str
            	}
			},
            {field: 'diskName', hide: true, sort: true, title: '盘点名称'},
            {field: 'warehouseName', hide: true, sort: true, title: '仓库名称'},
            {field: 'areaName', hide: true, sort: true, title: '库区名称'},
            {field: 'positionNumber', sort: true, title: '库位编号', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: '托盘编号', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: '商品名称', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: '编号', style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: '货号', style:'font-weight: 600;'},
            {field: 'goodsUnit', title: '单位'},
            {field: 'goodsPacking', title: '包装'},
            {field: 'goodsSpecs', title: '规格', totalRowText: '合计：'},
            {field: 'num', sort: true, title: '库存数', totalRow: true, style:'font-weight: 600;'},
            {field: 'volume', title: '库存体积', totalRow: true, style:'font-weight: 600;'},
            {field: 'trueNum', sort: true, title: '盘点数', totalRow: true,
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.trueNum + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.trueNum + '</span>'
					} 
            		return str
            	}
			},
            {field: 'trueVolume', title: '盘点体积', totalRow: true,
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.trueVolume + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.trueVolume + '</span>'
					} 
            		return str
            	}
			},
            {field: 'goodsbatchs', sort: true, title: '原批次', style:'font-weight: 600;'},
            {field: 'newGoodsbatchs', sort: true, title: '新批次',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.newGoodsbatchs + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.newGoodsbatchs + '</span>'
					} 
            		return str
            	}
			},
            {field: 'createTime', sort: true, title: '盘点时间',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.createTime + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.createTime + '</span>'
					} 
            		return str
            	}
			},
            {field: 'createUserName', title: '盘点用户',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.createUserName + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.createUserName + '</span>'
					} 
            		return str
            	}
			},
            {field: 'nodes', title: '盘点描述',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.nodes + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.nodes + '</span>'
					} 
            		return str
            	}
			},
            {field: 'dealTime', sort: true, title: '处理时间'},
            {field: 'dealPerson', sort: true, title: '处理用户'},
            {field: 'dealNodes', title: '处理备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作',fixed: 'right',width:80}
        ]];
    };

    /**
     * 点击查询按钮
     */
    PandianJihuaDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['inventory'] = $("#inventory").val();
        queryData['panType'] = $("#panType").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        table.reload(PandianJihuaDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    PandianJihuaDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/pandianJihuaDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    PandianJihuaDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(PandianJihuaDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    PandianJihuaDetailed.openEditDlg = function (data) {
	    layer.open({
	        type: 2,
	        title: '处理异常',
	        area: ['800px', '570px'],
	        anim: 5,
	        content: Feng.ctxPath + '/pandianJihuaDetailed/edit?id=' + data.id,
            end: function () {
                table.reload(PandianJihuaDetailed.tableId);
            }
	    });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PandianJihuaDetailed.tableId,
        url: Feng.ctxPath + '/pandianJihuaDetailed/list?diskId='+Feng.getUrlParam("diskId")+'&areaId='+Feng.getUrlParam("areaId")+'&createUser='+Feng.getUrlParam("createUser"),
        page: true,
        limit: 120,
		limits:[120,240,360,480],
        totalRow: true,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: PandianJihuaDetailed.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PandianJihuaDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        PandianJihuaDetailed.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        PandianJihuaDetailed.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + PandianJihuaDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            PandianJihuaDetailed.openEditDlg(data);
        } 
    });
});
